//
//  ChangeMyMessageViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/22.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "ChangeMyMessageViewController.h"
#import "TextFiledView.h"
#import "CanLab.h"
@interface ChangeMyMessageViewController ()
@property (nonatomic,strong)NSTimer *timer;//定时器
@property (nonatomic,assign)int s;//秒
@property (nonatomic,strong)UIButton *sendBtn;//验证码按钮
@property (nonatomic,strong)NSString *phoneStr;//手机号
@property (nonatomic,strong)NSString *codeStr;//验证码

@property (nonatomic,strong)TextFiledView *SexTextF;
@property (nonatomic,strong)UIView *chooseView;
@property (nonatomic,strong)UIButton *saveBtn;
@end

@implementation ChangeMyMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitleWithTitle:@"修改个人资料"];
    [self creadView];
}
-(void)viewWillAppear:(BOOL)animated
{
    [IQKeyboardManager sharedManager].enable = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [IQKeyboardManager sharedManager].enable = NO;
}

-(void)creadView
{
    NSArray *titles = @[@"昵称",@"性别"];
    TextFiledView *textF = [[TextFiledView alloc]initWithFrame:CGRectMake(0,5*screenW_6, SCREENW, 50*screenW_6) PlaceHodel:titles[0] Title:titles[0] text:[Storage getName]];
    textF.backgroundColor = [UIColor whiteColor];
    textF.tfBlock = ^(NSString *tfStr){
        _phoneStr = tfStr;
    };
    [self.view addSubview:textF];
    
    _SexTextF = [[TextFiledView alloc]initWithFrame:CGRectMake(0,55*screenW_6, SCREENW, 50*screenW_6) PlaceHodel:titles[1] Title:titles[1] text:[Storage getUserSex]];
    _SexTextF.backgroundColor = [UIColor whiteColor];
    _SexTextF.inputTf.enabled = NO;
    _SexTextF.inputTf.userInteractionEnabled = YES;
    [self.view addSubview:_SexTextF];
    UITapGestureRecognizer *sexTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sexSelectTap)];
    [_SexTextF addGestureRecognizer:sexTap];


    UIButton *btn = [TomamaUI createButton:0 Frame:CGRectMake(15*screenW_6, 135*screenW_6, SCREENW - 30*screenW_6, 45*screenW_6) targ:self sel:@selector(makeShare) titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:16*screenW_6] title:@"提交" image:nil backGroundImage:nil];
    btn.backgroundColor = greenColor;
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 3*screenW_6;
    [self.view addSubview:btn];
}

-(void)sexSelectTap//选择
{
    if (!_chooseView) {//非选择状态 弹出视图
        [self.view addSubview:self.chooseView];
    }else if (_chooseView.frame.size.height == 0)
    {   //height ＝ 0 弹出视图
        _chooseView.frame = CGRectMake(15*screenW_6,CGRectGetMaxY(_SexTextF.frame)+10*screenW_6, SCREENW-30*screenW_6,90*screenW_6);
    }
    else
    {   //收起视图
        _chooseView.frame = CGRectMake(15*screenW_6,CGRectGetMaxY(_SexTextF.frame)+10*screenW_6, SCREENW-30*screenW_6,0);
    }
    
}
-(UIView *)chooseView//创建选择View
{
    if (!_chooseView) {
        //底视图
        _chooseView = [[UIView alloc]initWithFrame:CGRectMake(15*screenW_6,CGRectGetMaxY(_SexTextF.frame)+10*screenW_6, SCREENW-30*screenW_6,90*screenW_6)];
        _chooseView.backgroundColor = [UIColor whiteColor];
        [_chooseView.layer setCornerRadius:5*screenW_6];
        _chooseView.layer.masksToBounds = YES;
        //男
        CanLab *boyLab = [[CanLab alloc]initWithFrame:CGRectMake(15*screenW_6,0, SCREENW-50*screenW_6,45*screenW_6) textAligment:0 font:16*screenW_6 textColor:@"#000000" text:@"男"];
        [_chooseView addSubview:boyLab];
        boyLab.block = ^(CanLab *lab){
            _chooseView.frame = CGRectMake(15*screenW_6,0, SCREENW-50*screenW_6,0);
            _SexTextF.inputTf.text = @"男";
        };
        //女
        [_chooseView addSubview:[ToolViewVC addCellLineLab:CGRectMake(15*screenW_6, 45*screenW_6, SCREENW-50*screenW_6, 1*screenW_6)]];
        
        CanLab *girlLab = [[CanLab alloc]initWithFrame:CGRectMake(15*screenW_6,46*screenW_6, SCREENW-50*screenW_6,44*screenW_6) textAligment:0 font:16*screenW_6 textColor:@"#000000" text:@"女"];
        [_chooseView addSubview:girlLab];
        girlLab.block = ^(CanLab *lab){
            _chooseView.frame = CGRectMake(15*screenW_6,0, SCREENW-50*screenW_6,0);
            _SexTextF.inputTf.text = @"女";
        };
    }
    return _chooseView;
}


#pragma mark - @selector

-(void)makeShare
{
    [self.view endEditing:YES];
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    //验证手机号
    if (_phoneStr.length == 0) {
        [HudHelper hideHudToView:self.view];
        [TomamaUI creatAlertWithTarget:self message:@"请填写昵称" timeInterval:2];
        return;
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:[Storage getToken] forKey:@"id"];
    [dic setObject:_phoneStr forKey:@"nickName"];
    [dic setObject:@"1" forKey:@"sex"];
    if ([_SexTextF.inputTf.text isEqualToString:@"女"]) {
        [dic setObject:@"2" forKey:@"sex"];
    }
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/login/updateUserInfo" Dic:dic completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([responseDic[@"success"] boolValue]) {
                    NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:[standfault objectForKey:@"userInfo"]];
                    [userInfo setObject:dic[@"sex"] forKey:@"sex"];
                    [userInfo setObject:dic[@"nickName"] forKey:@"nickName"];
                    [standfault setObject:userInfo forKey:@"userInfo"];
                    [standfault synchronize];
                    [TomamaUI creatAlertWithTarget:self message:@"发布成功" timeInterval:2 block:^{
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
                [HudHelper hideHudToView:self.view];
            });
        }];
    });
}

@end
